<template>
	<view v-if="goodsDetail.goods_price" class="goods-detail-container">
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
			<swiper-item v-for="(item,i) in goodsDetail.pics" :key="i">
				<image :src="item.pics_big" @click="pic(i)"></image>
			</swiper-item>
		</swiper>

		<!-- 商品信息 -->
		<view class="goods-info-box">
			<view class="price">&yen;{{goodsDetail.goods_price}}</view>
			<view class="goods-info-body">
				<view class="name">{{goodsDetail.goods_name}}</view>
				<view class="favi">
					<uni-icons type="star" size="22" color="gary"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">快递:免运费</view>
		</view>

		<!-- 商品介绍 -->
		<!-- <view v-html="goodsDetail.goods_introduce"></view> -->
		<rich-text :nodes="goodsDetail.goods_introduce"></rich-text>

		<!-- 工具栏 -->
		<uni-goods-nav class="goods_nav" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
			@buttonClick="buttonClick" />
	</view>
</template>
{item3.cat_id}
<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				goodsDetail: {},
				options: [{
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					info: 3
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				]
			};
		},
		onLoad(options) {
			console.log(options.goods_id);
			this.getgoodsDetail(options.goods_id)
			// this.options[1].info = this.getAllGoodsCount
		},
		methods: {
			// 把 m_cart 模块中的 addGoodsToCart 方法映射到当前页面使用
			...mapMutations('m_cart', ['addGoodsToCart']),
			async getgoodsDetail(goods_id) {
				const {
					data
				} = await uni.$http.get('/goods/detail', {
					goods_id
				})
				if (data.meta.status !== 200) return uni.$showToast()

				// webp: 谷歌的一种图片格式, ios不支持  为了兼容性 webp —> jpg
				data.message.goods_introduce = data.message.goods_introduce.replace(/webp/g, 'jpg')

				this.goodsDetail = data.message
			},
			pic(i) {
				const imgs = this.goodsDetail.pics.map(item => item.pics_big)
				uni.previewImage({
					current: i, // 索引
					urls: imgs, //需要预览的图片链接列表
				})
			},
			// 左侧按钮  点击购物车,跳转购物车页面
			onClick(e) {
				// console.log('a', e);
				if (e.index === 1) {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			// 右侧按钮
			buttonClick(e) {
				if (e.index === 0) {
					const goods = {
						goods_id: this.goodsDetail.goods_id,
						goods_name: this.goodsDetail.goods_name,
						goods_price: this.goodsDetail.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsDetail.goods_small_logo,
						goods_state: true
					}
					this.addGoodsToCart(goods)
					// this.options[1].info = this.getAllGoodsCount
				}
			},
		},
		computed: {
			// 调用 mapState方法,把 m_cart模块中的cart数组映射到当前页面中,作为计算属性来使用
			// ...mapState('模块的名称', ['要映射的数据名称1', '2'])
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['getAllGoodsCount'])
		},
		watch: {
			getAllGoodsCount: {
				handler() {
					this.options[1].info = this.getAllGoodsCount
				},
				immediate: true
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	// 商品信息区域的样式
	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				padding-right: 10px;
			}

			// 收藏区域
			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		// 运费
		.yf {
			margin: 10px 0;
			font-size: 12px;
			color: gray;
		}
	}

	.goods-detail-container {
		// 给页面外层的容器，添加 50px 的内padding，
		// 防止页面内容被底部的商品导航组件遮盖
		padding-bottom: 50px;
	}

	.goods_nav {
		// 为商品导航组件添加固定定位
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
</style>